Project: Monitoring Changes in Surface Water Using Satellite Image Data
Deliverable: Reading of a satellite image using Rasterio and manipulation of the image using keras and TensorFlow 2.0
The Co-ordinate Reference System is changed to the world coordinate system, EPSG 4326, while writing the JP2 file to GeoTiff format using rasterio.
The image manipulation is done in keras using ImageDataGenerator.
Another example of image manipulation provided at the end, uses tensorflow image method tf.image.crop_to_bounding_box to crop out a rectangular area from the originally loaded tiff image, into a new image and save it to disk.
Part_1_GettingStarted to download the satellite image from the google drive folder.import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import array_to_img
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from numpy import expand_dims
import rasterio
from rasterio import plot
import matplotlib.pyplot as plt
imagePath = 'example.jp2'
%run ../Scripts/download_script.py -i '1o76QoBtn6ExxO8KgcCqdOiun_KsWoMJl' -d imagePath
img = rasterio.open(imagePath, driver = 'JP2OpenJPEG')
def get_properties(image):
return img.profile
properties = get_properties(img)
display(properties)
plot.show(img)
_, axs = plt.subplots(1, 3, figsize = (10,6))
color_map = ['Reds', 'Greens', 'Blues']
for i in range(0, 3):
plot.show(img.read(i+1), ax = axs[i], cmap = color_map[i])
plt.tight_layout()
kwds = properties
kwds['driver'] = 'GTiff'
kwds['photometric'] = 'YCbCr'
kwds['compress'] = 'JPEG'
kwds['crs'] = 'EPSG:4326'
del kwds['transform']
kwds
def write_jp2_to_geotiff(img, output_file, parameters):
gtiff = rasterio.open(output_file, 'w',**parameters)
for band in range(1, parameters['count']+1):
gtiff.write(img.read(band), band)
gtiff.close()
output_file = 'ex.tiff'
write_jp2_to_geotiff(img, 'ex.tiff', kwds)
original_size = (kwds['height'], kwds['width'])
display(original_size)
scale = 4
new_size = (int(original_size[0]/scale), int(original_size[1]/scale))
new_size
load_img will load the image with the scaled down size provided as the argument into a PIL image.
That PIL will then be converted to numpy array with the call to img_to_array which would then be used for further transformation.
kimg = load_img(output_file, target_size = new_size)
display(kimg)